
(function() {

    const TEST_URLBASE = "chrome://lucidity/content/test/";

    function dumpKeyBindings()
    {
        var alpha = "abcdefghijklmnopqrstuvwxyz";
        var mainKeySet = document.getElementById("mainKeyset");
        var keyNodes = mainKeySet.getElementsByTagName("key");
        lucidapi.error("keyNodesCount: %s", keyNodes.length);
        for (var ii = 0; ii < keyNodes.length; ii++)
        {
            var modsattr = keyNodes[ii].getAttribute("modifiers");
            var keyattr = keyNodes[ii].getAttribute("key");
            if (keyattr)
            {
                keyattr = keyattr.toLowerCase();
                var idx = alpha.indexOf(keyattr);
                if (idx == 0)
                {
                    alpha = alpha.substr(1);
                } else if (idx > 0) {
                    alpha = alpha.substr(0,idx) + alpha.substr(idx+1);
                }
            }
            var keyelid = keyNodes[ii].getAttribute("id");
            var keycode = keyNodes[ii].getAttribute("keycode");
            var cmdattr = keyNodes[ii].getAttribute("command");
            lucidapi.info("key: id='%s' modifiers='%s' key='%s' command='%s'", keyelid, modsattr, keyattr ? keyattr : keycode, cmdattr);
        }
        lucidapi.error("alpha: %s", alpha);
    }


    function selfTest1()
    {
        var result = pgp_generateKeys("Woody","asdf",512,2048,60 * 60 * 24 * 365);
        var keyset = new PGPKeySet();
        var retval = keyset.importKeyData(result, false);
        lucidapi.assert(retval);
        lucidapi.assert(keyset.getSecretChain().length == 1);
        lucidapi.assert(keyset.getPublicChain().length == 1);
        var encKey = keyset.getPublicChain()[0];
        var plaintext = "Hello, there!";
        var filename = "foo.txt";
        var sigKeyId = keyset.getSecretChain()[0].getKeyId();
        var pcentries = {};
        pcentries[sigKeyId] = "asdf";
        pcentries[encKey.getCryptoKey().getKeyId()] = "asdf";
        var pcache = new PGPPassphraseProvider(pcentries);
        var cipherResult = pgp_encryptData(plaintext,keyset,{passCache:pcache,"filename":filename,"encKeyIds":encKey.getKeyId(),"sigKeyIds":sigKeyId,"clearsign":false,"format":"t"});
        lucidapi.assert(lucidapi.hasArmorType(cipherResult.m_result,"MESSAGE"));
        var plainResult = pgp_decryptData(cipherResult.m_result,keyset,{passCache:pcache});
        lucidapi.assert(plainResult.m_filename == filename);
        lucidapi.assert(plainResult.m_result == plaintext);
        lucidapi.assert(plainResult.m_siginfo.keyid == sigKeyId);
        lucidapi.assert(plainResult.m_siginfo.verified == true);
    }

    function selfTest2()
    {
        var keyset = new PGPKeySet();
        var secKeyData = lucid_getContent("chrome://lucidity/content/test/userA/sec-keys.asc");
        var retval = keyset.importKeyData(secKeyData, false);
        lucidapi.assert(retval);
        lucidapi.assert(keyset.getSecretChain().length == 2);
        lucidapi.assert(keyset.getPublicChain().length == 2);

        var dsaKeyId = "f64e2e34cdcdced9";
        var dsaSubId = "37d0d0844312a76a";
        var rsaKeyId = "2cb0b8bc1bac83a0";

        var pcentries = {};
        pcentries[dsaKeyId] = "asdf";
        pcentries[dsaSubId] = "asdf";
        pcentries[rsaKeyId] = "asdf";
        var pcache = new PGPPassphraseProvider(pcentries);

        var plaintext = lucid_getContent("chrome://lucidity/content/test/loremipsum.txt");
        var cipherText = lucid_getContent("chrome://lucidity/content/test/userA/text1-com2org-es.asc");
        var plainResult = pgp_decryptData(cipherText,keyset,{passCache:pcache});
        lucidapi.assert(plainResult.m_result == plaintext);
        lucidapi.assert(plainResult.m_siginfo.keyid == dsaKeyId);
        lucidapi.assert(plainResult.m_siginfo.verified == true);

        cipherText = lucid_getContent("chrome://lucidity/content/test/userA/text1-org2com-es.asc");
        plainResult = pgp_decryptData(cipherText,keyset,{passCache:pcache});
        lucidapi.assert(plainResult.m_result == plaintext);
        lucidapi.assert(plainResult.m_siginfo.keyid == rsaKeyId);
        lucidapi.assert(plainResult.m_siginfo.verified == true);

    /*
            lucidapi.debug("DSA keyid0: %s", keyset.theSecretChain()[0].getKeyId())
            lucidapi.debug("DSA keyid1: %s", keyset.theSecretChain()[0].getCryptoKey().getKeyId())
            lucidapi.debug("RSA keyid0: %s", keyset.theSecretChain()[1].getKeyId())
    */
    }

    function addTest(suite,func)
    {
        var sConstructor = func.toString();
        var aMatch = sConstructor.match(/\s*function (.*)\(/);
        if (aMatch != null)
        {
            var funcname = aMatch[ 1 ];
            suite.push(funcname);
            suite[funcname] = func;
        }
    }

    lucidapi.selfTest = function()
    {
        try {
            var tests = [];
            addTest(tests,selfTest1);
            addTest(tests,selfTest2);
            var start = (new Date()).getTime();
            for (var ii = 0; ii < tests.length; ii++)
            {
                var funcname = tests[ii];
                var func = tests[funcname];
                var teststart = (new Date()).getTime();
                lucidapi.debug("test %s started", funcname);
                try {
                    func();
                } catch (e) {
                    lucidapi.error(e);
                }
                var testend = (new Date()).getTime();
                var testtotalsecs = (testend - teststart) / 1000;
                lucidapi.debug("test %s finished: %d secs", funcname, testtotalsecs);
            }
        } catch (e) {
    //        lucidapi.promptError(e);
            lucidapi.error(e);
        }
        var end = (new Date()).getTime();
        var totalsecs = (end - start) / 1000;
        lucidapi.promptAlert(lucidapi.sprintf("Self Test took %d seconds", totalsecs));
    }

})();
